package com.webside.points.wechat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;





public class Signature {

	
    public static String getMD5Sign(Map<String,String> map){
    	Collection<String> keyset= map.keySet();  
        List<String> list = new ArrayList<String>(keyset);  
        
        //对key键值按字典升序排序  
        Collections.sort(list);
        
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {  
            String key = list.get(i);
            if(map.get(key) != null && map.get(key).length() > 0){
            	buffer.append(list.get(i)+"="+map.get(list.get(i)) +"&");
            }
        }

        buffer.append("key=" + WeChatConfig.API_KEY);
        //System.out.println("转换前：>>>"+buffer.toString());
       /* String value="";
       try {
			value= new String(buffer.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
       //System.out.println("转换后：>>>"+value);
        return MD5Util.MD5Encode(buffer.toString()).toUpperCase();
    }
    
    public static String getSHA1Sign(Map<String,String> map){
    	Collection<String> keyset= map.keySet();  
        List<String> list = new ArrayList<String>(keyset);  
        
        //对key键值按字典升序排序  
        Collections.sort(list);
        
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {  
            String key = list.get(i);
            if(map.get(key) != null && map.get(key).length() > 0){
            	buffer.append(list.get(i)+"="+map.get(list.get(i)));
            	if(i != list.size()-1){
            		buffer.append("&");
            	}
            }
        }

        return SHA1Util.SHA1Encode(buffer.toString());
    }


}
